//
//  CollectActivity.m
//  UI-豆瓣项目APP
//
//  Created by liuxin on 15/11/5.
//  Copyright © 2015年 孙鹏. All rights reserved.
//

#import "CollectActivity.h"
#import <sqlite3.h>
@implementation CollectActivity
+(instancetype)shareDataBase{
    static CollectActivity *dataBase = nil;
    if(dataBase == nil){
        dataBase = [[CollectActivity alloc]init];
    }
    return dataBase;
}
static sqlite3 *db = nil;
//sqlite数据库是基于C语言的底层数据库

//打开数据库
-(void)openDB{
    if(db != nil){
        NSLog(@"collectionActivity数据库已经打开了");
        return ;
    }
    NSString *sqlFile = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *sqlPath = [sqlFile stringByAppendingPathComponent:@"CollectActivity.sqlite"];
    NSLog(@"%@",sqlPath);
    //    需要转成utf8的中文编码
    BOOL result =  sqlite3_open(sqlPath.UTF8String, &db);
    if(result == SQLITE_OK){
        NSLog(@"yes 打开成功");
    }else{
        NSLog(@"no 打开失败");
    }
}
//建表
-(void)createTable{
    NSString *sql = @"CREATE TABLE IF NOT EXISTS CollectActivity(number INTEGER PRIMARY KEY AUTOINCREMENT,titlt TEXT,begin_time TEXT,end_time TEXT,name TEXT,category_name TEXT,address TEXT,content TEXT,userName TEXT)";
    BOOL result =  sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if(result == SQLITE_OK){
        NSLog(@"创建表成功");
    }
}
-(void)insertActivity:(Activitylist *)activity userName:(NSString *)userName{
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO CollectActivity (title,begin_time,end_time,name,category_name,address,content,userName)VALUES('%@','%@','%@','%@','%@','%@','%@','%@')",activity.title,activity.begin_time, activity.end_time,activity.category_name, activity.act.name,activity.address,activity.content, userName];
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if(result == SQLITE_OK){
        NSLog(@"插入成功");
    }else{
        NSLog(@"插入失败");
    }
}

@end
